package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyAddressAddBo;
import com.ruoyi.system.bo.ZsyyAddressQueryBo;
import com.ruoyi.system.bo.ZsyyAddressEditBo;
import com.ruoyi.system.domain.ZsyyAddress;
import com.ruoyi.system.mapper.ZsyyAddressMapper;
import com.ruoyi.system.vo.ZsyyAddressVo;
import com.ruoyi.system.service.IZsyyAddressService;
import com.ruoyi.system.mapper.ZsyyAddressMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户地址Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyAddressServiceImpl extends ServicePlusImpl<ZsyyAddressMapper, ZsyyAddress> implements IZsyyAddressService {
    @Autowired
    private ZsyyAddressMapper iZsyyAddressMapper;

    @Override
    public ZsyyAddressVo queryById(Long id){
        return getVoById(id, ZsyyAddressVo.class);
    }

    @Override
    public TableDataInfo<ZsyyAddressVo> queryPageList(ZsyyAddressQueryBo bo) {
        PagePlus<ZsyyAddress, ZsyyAddressVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyAddressVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyAddressVo> queryList(ZsyyAddressQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyAddressVo.class);
    }

    private LambdaQueryWrapper<ZsyyAddress> buildQueryWrapper(ZsyyAddressQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyAddress> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, ZsyyAddress::getCommunityId, bo.getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getCommunityName()), ZsyyAddress::getCommunityName, bo.getCommunityName());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), ZsyyAddress::getAddress, bo.getAddress());
        lqw.eq(StrUtil.isNotBlank(bo.getDefaultAddress()), ZsyyAddress::getDefaultAddress, bo.getDefaultAddress());
        lqw.eq(bo.getUserId() != null, ZsyyAddress::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyAddress::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getConsignee()), ZsyyAddress::getConsignee, bo.getConsignee());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyAddressAddBo bo) {
        ZsyyAddress add = BeanUtil.toBean(bo, ZsyyAddress.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyAddressEditBo bo) {
        ZsyyAddress update = BeanUtil.toBean(bo, ZsyyAddress.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyAddress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
